﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Member
    {
        public void Add(KeRui.Model.Member model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@GroupName", SqlDbType.NVarChar, 50), new SqlParameter("@Email", SqlDbType.VarChar, 50), new SqlParameter("@EmailState", SqlDbType.Int, 4), new SqlParameter("@RealName", SqlDbType.NVarChar, 20), new SqlParameter("@Birthday", SqlDbType.DateTime), new SqlParameter("@Sign", SqlDbType.NVarChar, 0xff), new SqlParameter("@QQ", SqlDbType.VarChar, 20), new SqlParameter("@MSN", SqlDbType.VarChar, 50), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateIP", SqlDbType.VarChar, 50), new SqlParameter("@Question", SqlDbType.NVarChar, 50), new SqlParameter("@Answer", SqlDbType.NVarChar, 50), new SqlParameter("@LoginTimes", SqlDbType.Int, 4), 
                new SqlParameter("@LastLoginTime", SqlDbType.DateTime), new SqlParameter("@LastLoginIP", SqlDbType.VarChar, 50), new SqlParameter("@ThisLoginTime", SqlDbType.DateTime), new SqlParameter("@ThisLoginIP", SqlDbType.VarChar, 50), new SqlParameter("@ActiveTime", SqlDbType.DateTime), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@IsVIP", SqlDbType.Int, 4), new SqlParameter("@VIPStartTime", SqlDbType.DateTime), new SqlParameter("@VIPEndTime", SqlDbType.DateTime)
             };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.GroupID;
            parameters[3].Value = model.GroupName;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.EmailState;
            parameters[6].Value = model.RealName;
            parameters[7].Value = model.Birthday;
            parameters[8].Value = model.Sign;
            parameters[9].Value = model.QQ;
            parameters[10].Value = model.MSN;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.CreateIP;
            parameters[13].Value = model.Question;
            parameters[14].Value = model.Answer;
            parameters[15].Value = model.LoginTimes;
            parameters[0x10].Value = model.LastLoginTime;
            parameters[0x11].Value = model.LastLoginIP;
            parameters[0x12].Value = model.ThisLoginTime;
            parameters[0x13].Value = model.ThisLoginIP;
            parameters[20].Value = model.ActiveTime;
            parameters[0x15].Value = model.State;
            parameters[0x16].Value = model.IsVIP;
            parameters[0x17].Value = model.VIPStartTime;
            parameters[0x18].Value = model.VIPEndTime;
            parameters[0x19].Value = model.Guid;
            DbHelperSQL.RunProcedure("Member_ADD", parameters, out num);
        }

        public int Add(KeRui.Model.Member model, string Password)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@CreateIP", SqlDbType.NVarChar, 50), new SqlParameter("@Password", SqlDbType.NVarChar, 50), new SqlParameter("@Email", SqlDbType.NVarChar, 50) };
            parameters[0].Value = model.Username;
            parameters[1].Value = model.CreateIP;
            parameters[2].Value = Password;
            parameters[3].Value = model.Email;
            return DbHelperSQL.RunProcedure("Member_ADD", parameters, out num);
        }

        public int AddReturnUserID(KeRui.Model.Member model, string Password)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@CreateIP", SqlDbType.NVarChar, 50), new SqlParameter("@Password", SqlDbType.NVarChar, 50), new SqlParameter("@State", SqlDbType.Int, 4) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.GroupID;
            parameters[3].Value = model.CreateIP;
            parameters[4].Value = Password;
            parameters[5].Value = model.State;
            DbHelperSQL.RunProcedure("Member_ADDReturnUserID", parameters, out num);
            if (num == 1)
            {
                return num;
            }
            return (int) parameters[0].Value;
        }

        public int ChageState(int UserID, int IsAdmin)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@IsAdmin", SqlDbType.Int, 4) };
            parameters[0].Value = UserID;
            parameters[1].Value = IsAdmin;
            return DbHelperSQL.RunProcedure("Member_ChangeState", parameters, out num);
        }

        public bool CheckEmailExist(string userName)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Email", SqlDbType.NVarChar, 20) };
            parameters[0].Value = userName;
            return (DbHelperSQL.RunProcedure("Member_CheckEmailExist", parameters, out num) > 0);
        }

        public void Delete(int UserID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = UserID;
            DbHelperSQL.RunProcedure("Member_Delete", parameters, out num);
        }

        public KeRui.Model.Member GetModel(int UserID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = UserID;
            KeRui.Model.Member member = new KeRui.Model.Member();
            DataSet set = DbHelperSQL.RunProcedure("Member_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    member.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                member.Username = set.Tables[0].Rows[0]["Username"].ToString();
                if (set.Tables[0].Rows[0]["GroupID"].ToString() != "")
                {
                    member.GroupID = int.Parse(set.Tables[0].Rows[0]["GroupID"].ToString());
                }
                member.GroupName = set.Tables[0].Rows[0]["GroupName"].ToString();
                member.Email = set.Tables[0].Rows[0]["Email"].ToString();
                if (set.Tables[0].Rows[0]["EmailState"].ToString() != "")
                {
                    member.EmailState = int.Parse(set.Tables[0].Rows[0]["EmailState"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    member.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                member.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                member.Question = set.Tables[0].Rows[0]["Question"].ToString();
                member.Answer = set.Tables[0].Rows[0]["Answer"].ToString();
                if (set.Tables[0].Rows[0]["LoginTimes"].ToString() != "")
                {
                    member.LoginTimes = int.Parse(set.Tables[0].Rows[0]["LoginTimes"].ToString());
                }
                if (set.Tables[0].Rows[0]["LastLoginTime"].ToString() != "")
                {
                    member.LastLoginTime = DateTime.Parse(set.Tables[0].Rows[0]["LastLoginTime"].ToString());
                }
                member.LastLoginIP = set.Tables[0].Rows[0]["LastLoginIP"].ToString();
                if (set.Tables[0].Rows[0]["ThisLoginTime"].ToString() != "")
                {
                    member.ThisLoginTime = DateTime.Parse(set.Tables[0].Rows[0]["ThisLoginTime"].ToString());
                }
                member.ThisLoginIP = set.Tables[0].Rows[0]["ThisLoginIP"].ToString();
                if (set.Tables[0].Rows[0]["ActiveTime"].ToString() != "")
                {
                    member.ActiveTime = DateTime.Parse(set.Tables[0].Rows[0]["ActiveTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    member.State = int.Parse(set.Tables[0].Rows[0]["State"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsVIP"].ToString() != "")
                {
                    member.IsVIP = int.Parse(set.Tables[0].Rows[0]["IsVIP"].ToString());
                }
                if (set.Tables[0].Rows[0]["VIPStartTime"].ToString() != "")
                {
                    member.VIPStartTime = DateTime.Parse(set.Tables[0].Rows[0]["VIPStartTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["VIPEndTime"].ToString() != "")
                {
                    member.VIPEndTime = DateTime.Parse(set.Tables[0].Rows[0]["VIPEndTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["Birthday"].ToString() != "")
                {
                    member.Birthday = DateTime.Parse(set.Tables[0].Rows[0]["Birthday"].ToString());
                }
                member.RealName = set.Tables[0].Rows[0]["RealName"].ToString();
                member.Sign = set.Tables[0].Rows[0]["Sign"].ToString();
                member.QQ = set.Tables[0].Rows[0]["QQ"].ToString();
                member.MSN = set.Tables[0].Rows[0]["MSN"].ToString();
                member.Guid = set.Tables[0].Rows[0]["Guid"].ToString();
                member.Integral = int.Parse(set.Tables[0].Rows[0]["Integral"].ToString());
                return member;
            }
            return null;
        }

        public int Update(KeRui.Model.Member model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@State", SqlDbType.Int, 4) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.GroupID;
            parameters[3].Value = model.State;
            return DbHelperSQL.RunProcedure("Member_Update", parameters, out num);
        }

        public int Update_AllInfo(KeRui.Model.Member model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@GroupName", SqlDbType.NVarChar, 50), new SqlParameter("@Email", SqlDbType.VarChar, 50), new SqlParameter("@EmailState", SqlDbType.Int, 4), new SqlParameter("@RealName", SqlDbType.NVarChar, 20), new SqlParameter("@Birthday", SqlDbType.DateTime), new SqlParameter("@Sign", SqlDbType.NVarChar, 0xff), new SqlParameter("@QQ", SqlDbType.VarChar, 20), new SqlParameter("@MSN", SqlDbType.VarChar, 50), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateIP", SqlDbType.VarChar, 50), new SqlParameter("@Question", SqlDbType.NVarChar, 50), new SqlParameter("@Answer", SqlDbType.NVarChar, 50), new SqlParameter("@LoginTimes", SqlDbType.Int, 4), 
                new SqlParameter("@LastLoginTime", SqlDbType.DateTime), new SqlParameter("@LastLoginIP", SqlDbType.VarChar, 50), new SqlParameter("@ThisLoginTime", SqlDbType.DateTime), new SqlParameter("@ThisLoginIP", SqlDbType.VarChar, 50), new SqlParameter("@ActiveTime", SqlDbType.DateTime), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@IsVIP", SqlDbType.Int, 4), new SqlParameter("@VIPStartTime", SqlDbType.DateTime), new SqlParameter("@VIPEndTime", SqlDbType.DateTime), new SqlParameter("@Guid", SqlDbType.VarChar, 0x24), new SqlParameter("@Integral", SqlDbType.Int, 4)
             };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.GroupID;
            parameters[3].Value = model.GroupName;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.EmailState;
            parameters[6].Value = model.RealName;
            parameters[7].Value = model.Birthday;
            parameters[8].Value = model.Sign;
            parameters[9].Value = model.QQ;
            parameters[10].Value = model.MSN;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.CreateIP;
            parameters[13].Value = model.Question;
            parameters[14].Value = model.Answer;
            parameters[15].Value = model.LoginTimes;
            parameters[0x10].Value = model.LastLoginTime;
            parameters[0x11].Value = model.LastLoginIP;
            parameters[0x12].Value = model.ThisLoginTime;
            parameters[0x13].Value = model.ThisLoginIP;
            parameters[20].Value = model.ActiveTime;
            parameters[0x15].Value = model.State;
            parameters[0x16].Value = model.IsVIP;
            parameters[0x17].Value = model.VIPStartTime;
            parameters[0x18].Value = model.VIPEndTime;
            parameters[0x19].Value = model.Guid;
            parameters[0x1a].Value = model.Integral;
            return DbHelperSQL.RunProcedure("Member_Update_AllInfo", parameters, out num);
        }

        public int UpdateBasicInfo(KeRui.Model.Member model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@Email", SqlDbType.VarChar, 50), new SqlParameter("@Birthday", SqlDbType.DateTime), new SqlParameter("@QQ", SqlDbType.VarChar, 20), new SqlParameter("@MSN", SqlDbType.VarChar, 50), new SqlParameter("@Question", SqlDbType.NVarChar, 100), new SqlParameter("@Answer", SqlDbType.NVarChar, 100), new SqlParameter("@RealName", SqlDbType.NVarChar, 20), new SqlParameter("@EmailState", SqlDbType.Int, 4) };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.Username;
            parameters[2].Value = model.Email;
            parameters[3].Value = model.Birthday;
            parameters[4].Value = model.QQ;
            parameters[5].Value = model.MSN;
            parameters[6].Value = model.Question;
            parameters[7].Value = model.Answer;
            parameters[8].Value = model.RealName;
            parameters[9].Value = model.EmailState;
            return DbHelperSQL.RunProcedure("Member_UpdateBasicInfo", parameters, out num);
        }

        public int UpdatePwd(string name, string question, string answer, string newpwd)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@Question", SqlDbType.NVarChar, 20), new SqlParameter("@Answer", SqlDbType.NVarChar, 20), new SqlParameter("@Password", SqlDbType.VarChar, 200) };
            parameters[0].Value = name;
            parameters[1].Value = question;
            parameters[2].Value = answer;
            parameters[3].Value = newpwd;
            return DbHelperSQL.RunProcedure("Member_UpdatePwdByUID", parameters, out num);
        }
    }
}

